<?php

/**
 * 
 * @author yuxun
 *
 */
class InsideService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'         => array('text' => 'ID', 'rule' => 'int'),
				'openid'     => array('text' => '账号'),
				'special_id' => array('text' => '专服'),
				'channel_id' => array('text' => '渠道'),
				'username'=> array('text' => '使用者姓名'),
				'create_time'=> array('text' => '添加时间'),
				'action'     => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$modInside = new ModInside();
		$result = $modInside->page($params);
		
		$condition = array();
		$condition['g'] = $params['g'];
		$modChannel = new ModChannel();
		$channel_array = $modChannel->select($condition);
		$channel_list = array();
		foreach ($channel_array as $key=>$val){
			$channel_list[$val['id']] = $val['channel_name'];
		}
		
		$modSpecialServer = new ModSpecialServer();
		$special_array = $modSpecialServer->select($condition);
		$special_list = array();
		foreach ($special_array as $key=>$val){
			$special_list[$val['id']] = $val['name'];
		}
			
		foreach($result['data'] as &$v){
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			$v['special_id'] = $special_list[$v['special_id']];
			$v['channel_id'] = $channel_list[$v['channel_id']];
			$v['action']   .= ' <a href="'.PubFunc::buildUrl('/oss/?r=inside/delete').'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';
		}
		unset($v);
		return $result;
	}
	
	public static function doAdd($params){
		$user = AccAuth::me();
		//根据渠道id查询服务器列表
		$modChannel = new ModChannel();
		$serverList = $modChannel->getserverforchannelId($params['channel_id'],$params['g']);
		if(count($serverList) == 0){
			return array('state' => false, 'msg' => '该渠道没有设置服务器，增加失败');
		}
		
		$params_send = array(
				'account' => $params['openid'],
				'status'  => 3,
		);
		$api = new ApiService($serverList[0]['server_id']);
		$result = $api->setAccountStatus($params_send);
		if(!$result['state']){
			return array('state' => false, 'msg' => '增加内玩账号请求失败:'.$result['msg']);
		}
		
// 		$flag = false;
// 		foreach ($serverList as $key=>$val){
// 			$params_send = array(
// 					'account' => $params['openid'],
// 					'status'  => 3,
// 			);
// 			$api = new ApiService($val['server_id']);
// 			$result = $api->setAccountStatus($params_send);
// 			if($result['state']){
// 				$flag = true;
// 			}

// 		}
		
// 		if(!$flag){
// 			return array('state' => false, 'msg' => '增加内玩账号请求失败');
// 		}
		
	
		$modInside = new ModInside();
		$modInside->platform      = 'test';
		$modInside->game          = $params['g'];
		$modInside->openid   = $params['openid'];
		$modInside->channel_id  = $params['channel_id'];
		$modInside->create_user   = $user['UserID'];
		$modInside->create_time   = time();
		$modInside->update_user   = $user['UserID'];
		$modInside->update_time   = time();
		
		$modInside->special_id  = $params['special_id'];
		$modInside->server_id  = $serverList[0]['server_id'];
		$modInside->username  = $params['username'];
		
		$result = $modInside->save();
		return array('state' => true, 'msg' => '成功增加内玩账号');
	}
	
	public static function delete($id){
		if(empty($id)) return false;
		
		$modInside = new ModInside();
		$obj = $modInside->findByPk($id);
        $info = $obj->getAttributes();
	    $channel_id = $info['channel_id'];
	    $game = PubBase::getCurrentGame();
	    $modChannel = new ModChannel();
	    $serverList = $modChannel->getserverforchannelId($channel_id,$game);
	    if(count($serverList) == 0){
	    	return array('state' => false, 'msg' => '该渠道没有设置服务器,删除失败');
	    }
	    
	    $params_send = array(
	    		'account' => $info['openid'],
	    		'status'  => 0,
	    );
	    $api = new ApiService($serverList[0]['server_id']);
	    $result = $api->setAccountStatus($params_send);
	    if(!$result['state']){
	    	return array('state' => false, 'msg' => '删除内玩账号请求失败:'.$result['msg']);
	    }
	    
// 	    $flag = false;
// 	    foreach ($serverList as $key=>$val){
// 	    	$params_send = array(
// 	    			'account' => $info['openid'],
// 	    			'status'  => 0,
// 	    	);
// 	    	$api = new ApiService($val['server_id']);
// 	    	$result = $api->setAccountStatus($params_send);
// 	    	//$result['state'] = true;
// 	    	if($result['state']){
// 	    		$flag = true;
// 	    	}
// 	    }
	    
// 	    if(!$flag){
// 	    	return array('state' => false, 'msg' => '删除内玩账号请求失败');
// 	    }
		
		$modInside->deleteByPk($id);
		return array('state' => true, 'msg' => '成功删除内玩账号');
	}
	
	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field, $val,$g){
		$modInside = new ModInside();
		$conut = $modInside->repeat($id, $field, $val,$g);
		return $conut;
	}
	
	


}